<template>
    <div class="header-wrapper border-bottom" id="header-item">
        <router-link tag="div" to="/" class="logo">
            <img src="@/assets/styles/images/Fantastic.png" alt="">
        </router-link>
        <div class="item">
            <router-link tag="span"
                         :to="item.path"
                         v-for="(item,index) in navItem"
                         :key="index"
                         active-class="active"
                         exact
            >
                {{ item.name }}
            </router-link>
        </div>
        <div class="login-box">
            <i class="iconfont" @click="addArticle">&#xe687;</i>
            <router-link tag="span" to="/login" v-if="notLogin">登录</router-link>
            <router-link tag="div" to="/home" class="avatar" v-if="canLogin">
                <img src="@/assets/styles/images/avatar2.jpg" alt="">
            </router-link>
        </div>
    </div>
</template>

<script>
    export default {
        name: "header-navbar",
        data() {
            return {
                navItem: [
                    {
                        name: '首页',
                        path: '/index'
                    },
                    {
                        name: '今日热门',
                        path: '/news'
                    },
                    {
                        name: '关于',
                        path: '/about'
                    }
                ],
                isLogin: false
            }
        },
        methods: {
            addArticle () {
                /**
                 * 验证用户 本地 token 是否存在，若不存在或者其他异常，
                 * 让用户前往登录 （token 过期 暂时没处理。）
                 */
                let _this = this;
                if (!localStorage.getItem('userToken') && !localStorage.getItem('isLogin')) {
                    _this.$message({
                        message: '登录异常，请先登录再继续操作',
                        type: 'warning'
                    });
                    localStorage.removeItem('userToken');
                    localStorage.removeItem('isLogin');
                    _this.$router.push('/login');
                } else {
                    _this.$router.push('/add')
                }
            }
        },
        computed: {
            notLogin() {
                return localStorage.isLogin !== 'true'
            },
            canLogin() {
                return localStorage.isLogin === 'true'
            }
        }
    }
</script>

<style lang="stylus" scoped>
    @import "~@/assets/styles/css/variable.styl"
    .header-wrapper
        display: flex;
        padding: $commonPadding;
        background #fff;
        box-shadow: 0 0 10px 5px #eaeaea;
        .logo
            width: 10%;
            img
                width: 100%;
                padding-top: 15px;
        .item
            flex: 4;
            line-height: 56px;
            color: #555;
            font-size: 14px;
            font-weight: 400;
            font-family: Microsoft Yahei;
            span
                padding-left: 30px
        .login-box
            flex: 2;
            line-height: 56px;
            text-align right;
            i
                margin-right: 30px;
                font-size: 18px;
            span
                display inline-block;
                width: 34px;
                color: #e85f5f;
                font-size: 14px;
            .avatar
                width: 10%;

                display: inline-block;
                img
                    width: 100%;
                    vertical-align: -8px;
                    -webkit-border-radius: 50%;
                    -moz-border-radius: 50%;
                    border-radius: 50%;

</style>